// 04myCenter/02set/01enterpriseSet/enterpriseRetrievePassword/enterpriseRetrievePassword.js
var co = require('../../../../resource/js/common.js')
var M = require('../../../../resource/js/md5.js')
var app = getApp()
var _this, _data, _code = '',_opt
var interval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
     time: '获取验证码', // 短信验证码
     currentTime: 60,
     smsCode: '',
     isPhone: '',//不显示页面的号码
     tempPhone: '',//显示页面的号码
     modifyPassword: false, //显示手机验证或者输入新密码
     account: '',//用户
     isVerificationCode: '',//验证码提示
     passWordTip: '',//输入新密码提示
     disabled: '',
     bizInfo:''//商家信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     _this = this
     _data = this.data
     _opt = options
     this.getBizInfo()
     
     //console.log('encryptionPhone===', encryptionPhone)
     
  },
  
   //获取商家信息
   getBizInfo: function () {
      var url = 'jyhs/Admin/getInfo'
      co.getShareApi(url, {
         Biz_ID: _opt.Biz_ID
      }, function (res) {
         if (res.code == 0) {
            console.log('res', res)
            var inPhone = res.data.Biz_Phone
            var encryptionPhone = inPhone.substring(0, 3) + "****" + inPhone.substring(8, 11)
            _this.setData({
               bizInfo: res.data,
               tempPhone: encryptionPhone,
               account: res.data.Biz_Account,
               isPhone: inPhone
            })
         }
      })
   },

  
   // 获取验证码
   getVerCode() {
      this.getCode();
      this.setData({
         disabled: true
      })
      co.getShareApi('api/Sms/getPhoneCode', {
         mobile: _data.isPhone,
         type: 'reset_pass',
         appid: 1400134010
      }, function (res) {
         // console.log("【获取返回数据】===", res.data)
         _this.setData({
            smsCode: res.data.Sms_Code
         })
      })
   },
   /*获取短信验证码倒计时*/
   getCode(options) {
      var currentTime = _data.currentTime
      interval = setInterval(function () {
         currentTime--;
         _this.setData({
            time: currentTime + '秒'
         })
         if (currentTime <= 0) {
            clearInterval(interval)
            _this.setData({
               time: '重新发送',
               currentTime: 120,
               disabled: false,
               smsCode: '',
            })
         }
      }, 1000)
   },
   //获取输入的验证码
   inputCode: function (e) {
      _code = e.detail.value
   },
   //验证短信验证码
   confirm: function () {
      var isVerificationCode = ''
      if (_code === '') {
         isVerificationCode = '验证码必填'
      } else if (_code !== _data.smsCode) {
         isVerificationCode = '验证码错误'
      } else if (_code === _data.smsCode) {
         clearInterval(interval)
         _this.setData({
            modifyPassword: true
         })
      }
      this.setData({
         isVerificationCode: isVerificationCode
      })
   },
   //获取输入的密码
   changePassword: function (e) {

      var url = 'jyhs/Admin/setPassword'
      var formData = e.detail.value
      var newPassword = formData.newPassword
      var newPsd = formData.newPsd
      var reg = /^[A-Za-z0-9]{6,20}$/
      var pwdTip = ''
      var param = {
         Biz_ID: _opt.Biz_ID,
         Biz_New_PayPassword: newPassword,
         type: 1
      }
      console.log('param===', param)
      console.log('newPassword', newPassword)
      if (newPassword === '' || newPsd === '') {
         pwdTip = '密码不能为空'
      } else if (newPassword !== newPsd) {
         pwdTip = '两次输入的密码不同'
      } else if (newPassword.length < 6) {
         pwdTip = '支付密码长度必须大于6位数'
      } else if (!reg.test(newPassword)) {
         pwdTip = '请正确输入新支付密码'
      } else {
         co.getShareApi(url, param, function (res) {
            console.log('res', res)
            if (res.code == 0) {
               co.confirm(res.msg, '', '确定', function (res) {
                  if (res) {
                     wx.navigateBack({
                        delta: 1
                     })
                  }
               })
            } else {
               co.Toast(res.msg)
            }
         })
      }
      _this.setData({
         passWordTip: pwdTip
      })
   },
   /**
   * 生命周期函数--监听页面初次渲染完成
   */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {
      clearInterval(interval)
   },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})